﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class RecipeListScript : MonoBehaviour {

    public Transform recipePanel, recipeIngredientPanel, detailIngredientPanel;

	// Use this for initialization
	void Start ()
    {
        RebuildCraftingList();
    }

    private void RebuildCraftingList()
    {
        Transform content = GameObject.Find("Content").transform;
        foreach (Transform t in content)
        {
            Destroy(t.gameObject);
        }

        foreach (Recipe r in SingletonManager.getRecipes())
        {
            if ((r.output.StartsWith("outfit") && PlayerInventory.ownedOutfits.Contains(SingletonManager.FindOutfit(r.output))) || (r.output.StartsWith("weapon") && PlayerInventory.ownedWeapons.Contains(SingletonManager.FindWeapon(r.output)))) continue;
            Transform created = Instantiate(recipePanel, content);
            created.Find("CraftedSprite").GetComponent<Image>().sprite = SingletonManager.GetOutputSprite(r);
            created.Find("Text").GetComponent<Text>().text = SingletonManager.Find(r.output).name;
            created.Find("CraftedSprite").GetComponent<Image>().preserveAspect = true;

            created.GetComponent<Button>().onClick.AddListener(() => ClickHandler(r.key));

            Transform collPanel = created.Find("IngredientsCollectionPanel");
            for (int i = 0; i < r.inputKeys.Length; i++)
            {
                Transform item = Instantiate(recipeIngredientPanel, collPanel);
                item.Find("IngredientSprite").GetComponent<Image>().sprite = SingletonManager.GetSprite(SingletonManager.FindMaterial(r.inputKeys[i]));
                item.Find("IngredientSprite").GetComponent<Image>().preserveAspect = true;
                item.Find("IngredientAmount").GetComponent<Text>().text = "x " + r.inputAmounts[i];
                if (!PlayerInventory.ContainsAmount(SingletonManager.FindMaterial(r.inputKeys[i]), r.inputAmounts[i])) item.Find("IngredientAmount").GetComponent<Text>().color = Color.red;
            }
        }
    }

    public void ClickHandler(string recipe_key)
    {
        Recipe r = SingletonManager.FindRecipe(recipe_key);
        GameObject dcp = GameObject.Find("Canvas").transform.Find("DoCraftPanel").gameObject;
        dcp.SetActive(true);

        foreach (Transform child in dcp.transform.Find("DoCraftInputCollection"))
        {
            Destroy(child.gameObject);
        }

        for (int i = 0; i < r.inputKeys.Length; i++)
        {
            Transform item = Instantiate(detailIngredientPanel, dcp.transform.Find("DoCraftInputCollection"));
            item.Find("MaterialSprite").GetComponent<Image>().sprite = SingletonManager.GetSprite(SingletonManager.FindMaterial(r.inputKeys[i]));
            item.Find("MaterialSprite").GetComponent<Image>().preserveAspect = true;
            item.Find("MaterialLabel").GetComponent<Text>().text = SingletonManager.FindMaterial(r.inputKeys[i]).name + "\nx" + r.inputAmounts[i];
            if (!PlayerInventory.ContainsAmount(SingletonManager.FindMaterial(r.inputKeys[i]), r.inputAmounts[i])) item.GetComponent<Image>().color = Color.red;
        }

        dcp.transform.Find("OutputSprite").GetComponent<Image>().sprite = SingletonManager.GetOutputSprite(r);
        dcp.transform.Find("OutputSprite").GetComponent<Image>().preserveAspect = true;
        dcp.transform.Find("OutputText").GetComponent<Text>().text = SingletonManager.Find(r.output).name + "\nx" + r.outputAmt;

        dcp.transform.Find("CraftButton").GetComponent<Button>().onClick.AddListener(() => DoCraft(recipe_key));

        if (PlayerInventory.CheckRecipe(SingletonManager.FindRecipe(recipe_key)))
        {
            dcp.transform.Find("CraftButton").GetComponent<Button>().interactable = true;
        }
        //Debug.Log("Recipe key is " + recipe_key);
    }

    public void DoCraft(string recipe_key)
    {
        PlayerInventory.RunRecipe(SingletonManager.FindRecipe(recipe_key));
        GameObject.Find("Canvas").transform.Find("DoCraftPanel").gameObject.SetActive(false);
        RebuildCraftingList();
    }

    public void HideCraftPanel()
    {
        GameObject.Find("DoCraftPanel").SetActive(false);
    }
	
}
